<?php

namespace app\admin\controller\redeem;

use app\common\controller\Backend;
use fast\Random;
use think\Db;
use Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 兑换码
 *
 * @icon fa fa-circle-o
 */
class Code extends Backend
{

    /**
     * Code模型对象
     * @var \app\admin\model\redeem\Code
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\redeem\Code;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','order_no','type','system_prop_json','num','di_json','zhe_json','remark','status','create_time','update_time']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['username','nickname']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

     /**
     * 添加
     *
     * @return string
     * @throws \think\Exception
     */
    public function add()
    {
        if (false === $this->request->isPost()) {
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);
        
        
        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
            $params[$this->dataLimitField] = $this->auth->id;
        }
        $system_prop_json = json_decode($params['system_prop_json'],true);
        $di_json = json_decode($params['di_json'],true);
        $zhe_json = json_decode($params['zhe_json'],true);
        $remark = '';
        if($params['num']){
            $remark = $params['num']."嘶豆";
        }

        if($system_prop_json){
            foreach($system_prop_json as $key => $val){
                $system_prop = DB::name("system_prop")->where("id",$val['id'])->find();
                if($remark){
                    $remark .= ",".$system_prop['name']."*".$val['num'];
                }else{
                    $remark = $system_prop['name']."*".$val['num'];
                }
            }
        }
        if($di_json){
            foreach($di_json as $k => $v){
                if($v['door']){
                    $msg = "抵扣券满".$v['door']."嘶豆减".$v['money']."嘶豆*".$v['num'];
                }else{
                    $msg = $v['money']."嘶豆抵扣券*".$v['num'];
                }
                if($remark){
                    $remark .= ",".$msg;
                }else{
                    $remark = $msg;
                }
            }
        }
        if($zhe_json){
            foreach($zhe_json as $kk => $vv){
                if($vv['door']){
                    $msg = $vv['ratio']."折特权券(满".$vv['door']."嘶豆)可用*".$vv['num'];
                }else{
                    $msg = $vv['ratio']."折特权券*".$vv['num'];
                }
                if($remark){
                    $remark .= ",".$msg;
                }else{
                    $remark = $msg;
                }
            }
        }
        // print_r($params);exit;
        for($i=1;$i<=$params['sheng_num'];$i++){
            if($params['order_no']){
                $order_no = $params['order_no'];
            }else{
                $order_no = date("YmdHis").Random::alnum(6);
            }
            $data[] = array(
                "order_no" => $order_no,
                "type" => $params['type'],
                "system_prop_json" => $params['system_prop_json'],
                "num" => $params['num'],
                "di_json" => $params['di_json'],
                "zhe_json" => $params['zhe_json'],
                "status" => 1,
                "remark" => $remark,
                "create_time" => date("Y-m-d H:i:s")
            );
        }
        DB::name("redeem_code")->insertAll($data);
        $this->success();
    }

}
